package com.ruoyi.web.controller.webgroup;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.webgroup.ClockCount;
import com.ruoyi.system.webgroupservcie.ClockCountService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 打卡统计时间的controller层
 */
@RestController
@Transactional
@RequestMapping("web/clockCount")
public class ClockCountController extends BaseController {

    @Autowired
    private ClockCountService clockCountService;

    @GetMapping ("/list")
    public TableDataInfo getClockCountAll(ClockCount clockCount){
        startPage();
        List<ClockCount> clockCountAll = clockCountService.getClockCountAll(clockCount);
        return getDataTable(clockCountAll);
    }

    /**
     * 导出日打卡记录列表
     */
    //@PreAuthorize("@ss.hasPermi('system:count:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClockCount webClockCount)
    {
        List<ClockCount> list = clockCountService.getClockCountAll(webClockCount);
        ExcelUtil<ClockCount> util = new ExcelUtil<ClockCount>(ClockCount.class);
        util.exportExcel(response, list, "导出日打卡记录列表数据");
    }


    /**根据用户id查询*/
    @GetMapping("/{clockCountId}")
    public AjaxResult getClockCountById(@PathVariable("clockCountId")Integer clockCountId){
        ClockCount clockCount = clockCountService.getClockCountById(clockCountId);
        return AjaxResult.success(clockCount);
    }
    /**根据表单提交的数据新增数据*/
    @PostMapping
    public AjaxResult postClockCountById(@RequestBody ClockCount clockCount){
        if (clockCount != null){
            clockCountService.postClockCountById(clockCount);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
    /**修改某个数数据*/
    @PutMapping
    public AjaxResult putClockCountById(@RequestBody ClockCount clockCount){
        if (clockCount.getClockCountId() != null){
            clockCountService.putClockCountById(clockCount);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
    /**根据该表的主键clockCountId删除*/
    @DeleteMapping("/clockCountById/{clockCountId}")
    public AjaxResult deleteClockCountById(@PathVariable("clockCountId")Integer clockCountId){
        try{
            clockCountService.deleteClockCountById(clockCountId);
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error();
        }
    }

    /**日排行榜*/
    @GetMapping("/dailyRankingEdition/{clockCountDate}")
    public AjaxResult dailyRankingEdition(@PathVariable("clockCountDate")String clockCountDate){
        /**首先将一天时间段加起来然后进行排行
         * 要查出序列（排第几）
         * 名字
         * 总时长
         */
        Date date = DateUtils.parseDate(clockCountDate);
        List<ClockCount> clockCount = clockCountService.dailyRankingEdition(date);
        return AjaxResult.success(clockCount);
    }

}
